//
//  RechargeMentionDetailsVC.m
//  MRBang
//
//  Created by 铅笔 on 16/5/12.
//  Copyright © 2016年 杭州易网新科技. All rights reserved.
//

#import "RechargeMentionDetailsVC.h"
#import "MyTradingRecordCell.h"
#import "RechargeMentionDetailsModel.h"

#define CELLHEIGHT 80*PROPORTION_WIDTH

static int from =0;

@interface RechargeMentionDetailsVC ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) UITableView *tableView_detail;

@property (nonatomic,strong) NSMutableArray *mutableArray_data;//存放所有数

@end

@implementation RechargeMentionDetailsVC

- (NSMutableArray *)mutableArray_data
{
    if (!_mutableArray_data) {
        _mutableArray_data = [[NSMutableArray alloc] init];
    }
    return _mutableArray_data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"交易明细";
    self.view.backgroundColor = COLOR_EVERYVIEWBG;
     [self initWithRechargeMentionDetailsVCSubviews];
    
    //网络数据请求
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self DataSourceRequestRechargeMentionDetailsListView];
}

#pragma mark --- 创建明细表
- (void) initWithRechargeMentionDetailsVCSubviews
{
    self.tableView_detail = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth,kScreenHeight+64)];
    [self.view addSubview:self.tableView_detail];
    self.tableView_detail.dataSource = self;
    self.tableView_detail.delegate = self;
    self.tableView_detail.showsVerticalScrollIndicator = NO;
    self.tableView_detail.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView_detail.backgroundColor = COLOR_EVERYVIEWBG;
    
    //上下拉刷新
    [self setUprefresh];
}

#pragma mark --- UITableViewDataSource
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.mutableArray_data.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellMyTradingRecordID = @"cellMyTradingRecordID";
    MyTradingRecordCell *cell_myTradingRecord = [tableView dequeueReusableCellWithIdentifier:cellMyTradingRecordID];
    if (cell_myTradingRecord == nil) {
        cell_myTradingRecord = [[MyTradingRecordCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellMyTradingRecordID];
    }
    
    UIView *view_line = [[UIView alloc] initWithFrame:CGRectMake(27*PROPORTION_WIDTH, CELLHEIGHT-1, kScreenWidth, 1.0)];
    [cell_myTradingRecord.contentView addSubview:view_line];
    view_line.backgroundColor = [BaseViewController colorWithHexString:@"#EEEFF1"];
    
    cell_myTradingRecord.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if (self.mutableArray_data.count!=0) {
                
        [cell_myTradingRecord initWithRechargeMentionDetailsModel:self.mutableArray_data[indexPath.row]];
    }
    
    return cell_myTradingRecord;
    
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 80*PROPORTION_WIDTH;
}

#pragma mark - 集成下拉刷洗 上拉加载
- (void)setUprefresh
{
    __weak __typeof(self) weakSelf = self;
    
    // 设置回调（一旦进入刷新状态就会调用这个refreshingBlock）
    self.tableView_detail.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [weakSelf loadNewData];
    }];
    
    // 马上进入刷新状态
    //    [self.tableView.header beginRefreshing];
}

#pragma mark - 数据处理相关
#pragma mark 下拉刷新数据
- (void)loadNewData
{
    from=0;
    
    [self DataSourceRequestRechargeMentionDetailsListView];
    
    // 拿到当前的下拉刷新控件，结束刷新状态
    [self.tableView_detail.header endRefreshing];
    
}

#pragma mark -- 网络数据请求
- (void) DataSourceRequestRechargeMentionDetailsListView
{
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    NetworkStatus status = [reach currentReachabilityStatus];
    
    if (![UIView NetFromStatus:status]) {
        
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [UIView showAlertMsg:@"网络连接已断开" andShowBgColor:[UIColor blackColor] andLabelTectColor:[UIColor whiteColor]];
        
        return;
    }
    else
    {
        NSString *strUrl = [NSString stringWithFormat:@"%@member/withdrawcash",MRAPI];
        NSDictionary *paras_loan = @{@"memberid":[[NSUserDefaults standardUserDefaults] objectForKey:@"memberid"]};
        
        [[HttpRequest shardWebUtil] getNetworkRequestURLString:strUrl parameters:paras_loan success:^(id obj) {
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            
            NSLog(@"withdrawcash%@",obj);
            
            NSString *str_false = obj[@"error"];
            
            if (str_false.length==0) {
                
                [self.mutableArray_data removeAllObjects];
                NSArray *array_data = obj[@"data"];
                if (array_data.count!=0) {
                    for (int i=0; i<array_data.count; i++) {
                        RechargeMentionDetailsModel *model = [[RechargeMentionDetailsModel alloc] init];
                        [model decodeDic:array_data[i]];
                        [self.mutableArray_data addObject:model];
                    }
                    
                    [self.tableView_detail reloadData];
                }
                else
                {
                    [BaseViewController showMessage:@"暂无数据"];
                }
                
            }
            else
            {
                [BaseViewController showMessage:str_false];
            }
            
        } fail:^(NSError *error) {
            NSLog(@"get%@",error);
            
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [UIView showAlertMsg:@"网络请求超时"];
            
        }];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
